using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using AutumnEngine.Modules;
using System.Diagnostics;
using AutumnEngine;

namespace AutumnEngine.Modules
{
    /// <summary>
    /// Manages the graphics device.
    /// </summary>
    public class GraphicsManagerModule : Module
    {
        /// <summary>
        /// Creates this instance.
        /// </summary>
        public override void Create()
        {
            this.Removable = false;
            this.CanBeDisabled = false;

#if WINDOWS
            Resolution = new Vector2(1280,720);      
#else

            Resolution = new Vector2(272,480);       
#endif
            base.Create();
        }

        public override void Draw()
        {
            Engine.GraphicsDeviceManager.GraphicsDevice.Clear(Engine.Modules.Settings.ClearColor);
            base.Draw();
        }

        bool fullScreen = false;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="GraphicsManagerModule"/> is fullscreen.
        /// </summary>
        /// <value><c>true</c> if fullscreen; otherwise, <c>false</c>.</value>
        public bool Fullscreen
        {
            get { return fullScreen; }
            set
            {
                if (value != fullScreen)
                {
#if WINDOWS
                    Engine.GraphicsDeviceManager.ToggleFullScreen();
#endif
                }
                fullScreen = value;
            }
                
        }

        Vector2 resolution;
        /// <summary>
        /// Gets or sets the resolution.
        /// </summary>
        /// <value>The resolution.</value>
        public Vector2 Resolution
        {
            get { return resolution; }
            set
            {
                resolution = value;
                ResolutionChanged();
            }
        }

        /// <summary>
        /// Resolutions the changed.
        /// </summary>
        void ResolutionChanged()
        {
            if (Engine.Modules.Contains("Engine.Logging"))
                Engine.Modules.Logging.Write(String.Format("Changing resolution to {0} x {1}.", resolution.X, resolution.Y),LogLevel.Debug);
            Engine.GraphicsDeviceManager.PreferredBackBufferHeight = (int)resolution.Y;
            Engine.GraphicsDeviceManager.PreferredBackBufferWidth = (int)resolution.X;
            Engine.GraphicsDeviceManager.ApplyChanges();
        }
    }
}
